package com.my.controller;

import com.my.mapper.RightColumnMapper;
import com.my.mapper.RightMapper;
import com.my.pojo.Right;
import com.my.pojo.RightColumn;
import com.my.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/right")
public class RightController extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        resp.setContentType("text/html;charset=utf-8");
        String action = req.getParameter("action");
        String result = "";
        SqlSession sqlSession = MybatisUtil.openSession();

        if (action.equals("preAdd")) {
            RightColumnMapper rightColumnMapper = sqlSession.getMapper(RightColumnMapper.class);
            List<RightColumn> rightColumnList = rightColumnMapper.searcherAllRightColumn();
            if (rightColumnList == null) {
                result = "error.jsp";
            } else {
                session.setAttribute("allRightColumn", rightColumnList);
                result = "right/addRight.jsp";
            }
        }

        if (action.equals("add")) {
            String rightColumnIdStr = req.getParameter("rightColumnId");
            int rightColumnId = Integer.parseInt(rightColumnIdStr);
            String rightName = req.getParameter("rightName");
            String rightDesc = req.getParameter("rightDesc");
            String url = req.getParameter("url");
            Right right = Right.builder().rightColumnId(rightColumnId).rightName(rightName).rightDesc(rightDesc).url(url).build();
            RightMapper rightMapper = sqlSession.getMapper(RightMapper.class);
            Boolean existRight = rightMapper.isExistRight(rightName);
            if (existRight == null) {
                rightMapper.addRight(right);
                sqlSession.commit();
                List<Right> list = new ArrayList<Right>();
                list = this.searchAllRightColumnAndRight();
                if (list == null) {
                    result = "error.jsp";
                } else {
                    session.setAttribute("allRight", list);
                    result = "right/showAllRight.jsp";
                }
            } else {
                result = "error.jsp";
            }
        }
        if (action.equals("showAllRight")) {
            List<Right> list = new ArrayList<Right>();
            list = this.searchAllRightColumnAndRight();
            if (list == null) {
                result = "error.jsp";
            } else {
                session.setAttribute("allRight", list);
                result = "right/showAllRight.jsp";
            }
        }
        resp.sendRedirect(result);
    }

    private List<Right> searchAllRightColumnAndRight() throws IOException {
        SqlSession sqlSession = MybatisUtil.openSession();
        RightColumnMapper rightColumnMapper = sqlSession.getMapper(RightColumnMapper.class);
        List<RightColumn> rightColumnList = rightColumnMapper.searcherAllRightColumn();
        RightMapper rightMapper = sqlSession.getMapper(RightMapper.class);
        List list3 = new ArrayList();
        for (int i = 0; i < rightColumnList.size(); i++) {
            RightColumn rightColumn = rightColumnList.get(i);
            String rightColumnName = rightColumn.getRightColumnName();
            int rightColumnId = rightColumn.getId();
            List<Right> rightList = rightMapper.searchAllRightColumnId(rightColumnId);
            List list1 = new ArrayList();
            List list2 = new ArrayList();
            list1.add(rightColumnName);
            list2.add(list1);
            list2.add(rightList);
            list3.add(list2);
        }
        return list3;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}